package com.example.javamaday.offer;

import com.example.javamaday.leetcode.base.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class GetOffer52II {

    public TreeNode increasingBST(TreeNode root) {
        List<Integer> list = inorderTraversal(root);
        if (list.size() == 0) {
            return null;
        }
        TreeNode treeNode = new TreeNode(list.get(0));
        TreeNode currentNod = treeNode;
        for (int i = 1; i < list.size(); i++) {
            currentNod.right = new TreeNode(list.get(i));
            currentNod = currentNod.right;
        }
        return treeNode;
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        ArrayList<Integer> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        Stack<TreeNode> treeNodeStack = new Stack<>();
        while(root != null || !treeNodeStack.isEmpty()) {
            if (root != null) {
                treeNodeStack.push(root);
                root = root.left;
            } else {
                root = treeNodeStack.pop();
                result.add(root.val);
                root = root.right;
            }
        }
        return result;
    }
}
